﻿using System;
using System.Collections.Generic;
using UnityEngine;

// Token: 0x020000A0 RID: 160
public class MMD4MecanimAnimMorphHelper : MonoBehaviour, MMD4MecanimAnim.IAnimModel
{
	// Token: 0x17000091 RID: 145
	// (get) Token: 0x0600039D RID: 925 RVA: 0x0001CCA9 File Offset: 0x0001AEA9
	public virtual bool isProcessing
	{
		get
		{
			return this._IsPlayingAnim() || this._inactiveModelMorphSet.Count != 0;
		}
	}

	// Token: 0x17000092 RID: 146
	// (get) Token: 0x0600039E RID: 926 RVA: 0x0001CCA9 File Offset: 0x0001AEA9
	public virtual bool isAnimating
	{
		get
		{
			return this._IsPlayingAnim() || this._inactiveModelMorphSet.Count != 0;
		}
	}

	// Token: 0x0600039F RID: 927 RVA: 0x0001CCCB File Offset: 0x0001AECB
	public void PlayAnim(string animName)
	{
		this.animName = animName;
	}

	// Token: 0x060003A0 RID: 928 RVA: 0x0001CCD4 File Offset: 0x0001AED4
	public void StopAnim()
	{
		this.playingAnimName = string.Empty;
	}

	// Token: 0x060003A1 RID: 929 RVA: 0x0001CCE1 File Offset: 0x0001AEE1
	MMD4MecanimAnim.IMorph MMD4MecanimAnim.IAnimModel.GetMorph(string name)
	{
		if (this._model != null)
		{
			return this._model.GetMorph(name);
		}
		return null;
	}

	// Token: 0x060003A2 RID: 930 RVA: 0x0001CD02 File Offset: 0x0001AF02
	MMD4MecanimAnim.IMorph MMD4MecanimAnim.IAnimModel.GetMorph(string name, bool startsWith)
	{
		if (this._model != null)
		{
			return this._model.GetMorph(name, startsWith);
		}
		return null;
	}

	// Token: 0x1700008A RID: 138
	// (get) Token: 0x060003A3 RID: 931 RVA: 0x0001CD24 File Offset: 0x0001AF24
	int MMD4MecanimAnim.IAnimModel.morphCount
	{
		get
		{
			if (this._model != null && this._model.morphList != null)
			{
				return this._model.morphList.Length;
			}
			return 0;
		}
	}

	// Token: 0x060003A4 RID: 932 RVA: 0x0001CD58 File Offset: 0x0001AF58
	MMD4MecanimAnim.IMorph MMD4MecanimAnim.IAnimModel.GetMorphAt(int index)
	{
		if (this._model != null && this._model.morphList != null && index >= 0 && index < this._model.morphList.Length)
		{
			return this._model.morphList[index];
		}
		return null;
	}

	// Token: 0x1700008B RID: 139
	// (get) Token: 0x060003A5 RID: 933 RVA: 0x0001CDAF File Offset: 0x0001AFAF
	int MMD4MecanimAnim.IAnimModel.animCount
	{
		get
		{
			if (this.animList != null)
			{
				return this.animList.Length;
			}
			return 0;
		}
	}

	// Token: 0x060003A6 RID: 934 RVA: 0x0001CDC6 File Offset: 0x0001AFC6
	MMD4MecanimAnim.IAnim MMD4MecanimAnim.IAnimModel.GetAnimAt(int index)
	{
		if (this.animList != null && index >= 0 && index < this.animList.Length)
		{
			return this.animList[index];
		}
		return null;
	}

	// Token: 0x1700008C RID: 140
	// (get) Token: 0x060003A7 RID: 935 RVA: 0x00019BD8 File Offset: 0x00017DD8
	Animator MMD4MecanimAnim.IAnimModel.animator
	{
		get
		{
			return null;
		}
	}

	// Token: 0x1700008D RID: 141
	// (get) Token: 0x060003A8 RID: 936 RVA: 0x0001CDF2 File Offset: 0x0001AFF2
	AudioSource MMD4MecanimAnim.IAnimModel.audioSource
	{
		get
		{
			if (this._audioSource == null)
			{
				this._audioSource = MMD4MecanimCommon.WeakAddComponent<AudioSource>(base.gameObject);
			}
			return this._audioSource;
		}
	}

	// Token: 0x1700008E RID: 142
	// (get) Token: 0x060003A9 RID: 937 RVA: 0x0001CE1C File Offset: 0x0001B01C
	bool MMD4MecanimAnim.IAnimModel.animSyncToAudio
	{
		get
		{
			return this.animSyncToAudio;
		}
	}

	// Token: 0x1700008F RID: 143
	// (get) Token: 0x060003AA RID: 938 RVA: 0x0001CE24 File Offset: 0x0001B024
	// (set) Token: 0x060003AB RID: 939 RVA: 0x00002973 File Offset: 0x00000B73
	float MMD4MecanimAnim.IAnimModel.prevDeltaTime
	{
		get
		{
			return 0f;
		}
		set
		{
		}
	}

	// Token: 0x17000090 RID: 144
	// (get) Token: 0x060003AC RID: 940 RVA: 0x0001CE2B File Offset: 0x0001B02B
	// (set) Token: 0x060003AD RID: 941 RVA: 0x0001CE33 File Offset: 0x0001B033
	MMD4MecanimAnim.IAnim MMD4MecanimAnim.IAnimModel.playingAnim
	{
		get
		{
			return this._playingAnim;
		}
		set
		{
			this._playingAnim = (MMD4MecanimAnimMorphHelper.Anim)value;
		}
	}

	// Token: 0x060003AE RID: 942 RVA: 0x0001CE41 File Offset: 0x0001B041
	void MMD4MecanimAnim.IAnimModel._SetAnimMorphWeight(MMD4MecanimAnim.IMorph morph, float weight)
	{
		morph.weight = ((this._morphWeight == 1f) ? weight : (weight * this._morphWeight));
	}

	// Token: 0x060003AF RID: 943 RVA: 0x0001CE67 File Offset: 0x0001B067
	private void Awake()
	{
		if (this.initializeOnAwake)
		{
			this._Initialize();
		}
	}

	// Token: 0x060003B0 RID: 944 RVA: 0x0001CE7A File Offset: 0x0001B07A
	private void Start()
	{
		this._Initialize();
	}

	// Token: 0x060003B1 RID: 945 RVA: 0x0001CE82 File Offset: 0x0001B082
	private void Update()
	{
		this._UpdateAnim();
		this._UpdateMorph();
	}

	// Token: 0x060003B2 RID: 946 RVA: 0x0001CE90 File Offset: 0x0001B090
	private void _Initialize()
	{
		if (this._initialized)
		{
			return;
		}
		this._initialized = true;
		this._model = base.gameObject.GetComponent<MMD4MecanimModel>();
		if (this._model == null)
		{
			return;
		}
		this._model.Initialize();
		MMD4MecanimAnim.InitializeAnimModel(this);
	}

	// Token: 0x060003B3 RID: 947 RVA: 0x0001CEE4 File Offset: 0x0001B0E4
	private void _UpdateAnim()
	{
		if (!this.animEnabled)
		{
			this._StopAnim();
			return;
		}
		if (this._playingAnim != null && (string.IsNullOrEmpty(this.playingAnimName) || this._playingAnim.animName == null || this.playingAnimName != this._playingAnim.animName))
		{
			this._StopAnim();
		}
		bool flag = false;
		if (this._playingAnim == null && !string.IsNullOrEmpty(this.animName) && this.animList != null)
		{
			for (int num = 0; num != this.animList.Length; num++)
			{
				if (this.animList[num].animName != null && this.animList[num].animName == this.animName)
				{
					this._PlayAnim(this.animList[num]);
					flag = true;
					break;
				}
			}
		}
		if (this._playingAnim != null)
		{
			if (this._playingAnim.animData != null)
			{
				if (!this.animPauseOnEnd && this._animTime >= (float)(this._playingAnim.animData.maxFrame - 1) / 30f)
				{
					this._StopAnim();
				}
			}
			else
			{
				this._StopAnim();
			}
		}
		if (this._playingAnim == null)
		{
			return;
		}
		if (!flag)
		{
			MMD4MecanimAnim.UpdateAnimModel(this, this._playingAnim, this._animTime);
		}
		if (this._playingAnim != null)
		{
			if (this._playingAnim.morphMotionList != null)
			{
				for (int num2 = 0; num2 != this._playingAnim.morphMotionList.Length; num2++)
				{
					if (this._playingAnim.morphMotionList[num2].morph != null)
					{
						((MMD4MecanimModel.Morph)this._playingAnim.morphMotionList[num2].morph).weight2 = this._weight2;
					}
				}
			}
			if (this._playingAnim.audioClip != null && this._audioSource != null && this._audioSource.isPlaying && this.animSyncToAudio)
			{
				this._animTime = this._audioSource.time;
			}
			else
			{
				this._animTime += Time.deltaTime;
			}
			if (this._playingAnim.animData != null)
			{
				this._animTime = Mathf.Min(this._animTime, (float)this._playingAnim.animData.maxFrame / 30f);
			}
			else
			{
				this._animTime = 0f;
			}
		}
	}

	// Token: 0x060003B4 RID: 948 RVA: 0x0001D17F File Offset: 0x0001B37F
	private bool _IsPlayingAnim()
	{
		return this._playingAnim != null && this._playingAnim.animData != null && this._animTime < (float)(this._playingAnim.animData.maxFrame - 1) / 30f;
	}

	// Token: 0x060003B5 RID: 949 RVA: 0x0001D1C0 File Offset: 0x0001B3C0
	private void _PlayAnim(MMD4MecanimAnimMorphHelper.Anim anim)
	{
		this._StopAnim();
		this._animTime = 0f;
		this.animName = string.Empty;
		if (anim != null)
		{
			this.playingAnimName = anim.animName;
		}
		MMD4MecanimAnim.UpdateAnimModel(this, anim, this._animTime);
		if (this._playingAnim != null && this._inactiveModelMorphSet != null && this._playingAnim.morphMotionList != null)
		{
			for (int num = 0; num != this._playingAnim.morphMotionList.Length; num++)
			{
				this._playingAnim.morphMotionList[num].lastKeyFrameIndex = 0;
				MMD4MecanimModel.Morph morph = (MMD4MecanimModel.Morph)this._playingAnim.morphMotionList[num].morph;
				if (morph != null)
				{
					this._inactiveModelMorphSet.Remove(morph);
				}
			}
		}
	}

	// Token: 0x060003B6 RID: 950 RVA: 0x0001D294 File Offset: 0x0001B494
	private void _StopAnim()
	{
		if (this._playingAnim != null && this._inactiveModelMorphSet != null && this._playingAnim.morphMotionList != null)
		{
			for (int num = 0; num != this._playingAnim.morphMotionList.Length; num++)
			{
				this._playingAnim.morphMotionList[num].lastKeyFrameIndex = 0;
				MMD4MecanimModel.Morph morph = (MMD4MecanimModel.Morph)this._playingAnim.morphMotionList[num].morph;
				if (morph != null && (morph.weight != 0f || morph.weight2 != 0f))
				{
					this._inactiveModelMorphSet.Add(morph);
				}
			}
		}
		MMD4MecanimAnim.StopAnimModel(this);
		this._animTime = 0f;
		this.playingAnimName = string.Empty;
	}

	// Token: 0x060003B7 RID: 951 RVA: 0x0001D368 File Offset: 0x0001B568
	private void _UpdateMorph()
	{
		float step = 1f;
		if (this.morphSpeed > 0f)
		{
			step = Time.deltaTime / this.morphSpeed;
		}
		if (this._playingAnim != null)
		{
			MMD4MecanimCommon.Approx(ref this._morphWeight, 1f, step);
			MMD4MecanimCommon.Approx(ref this._weight2, (!this.overrideWeight) ? 0f : 1f, step);
		}
		else
		{
			MMD4MecanimCommon.Approx(ref this._morphWeight, 0f, step);
			MMD4MecanimCommon.Approx(ref this._weight2, 0f, step);
		}
		if (this._inactiveModelMorphSet != null)
		{
			foreach (MMD4MecanimModel.Morph morph in this._inactiveModelMorphSet)
			{
				MMD4MecanimCommon.Approx(ref morph.weight, 0f, step);
				MMD4MecanimCommon.Approx(ref morph.weight2, 0f, step);
			}
			this._inactiveModelMorphSet.RemoveWhere((MMD4MecanimModel.Morph s) => s.weight == 0f && s.weight2 == 0f);
		}
	}

	// Token: 0x04000517 RID: 1303
	public string animName = string.Empty;

	// Token: 0x04000518 RID: 1304
	public string playingAnimName = string.Empty;

	// Token: 0x04000519 RID: 1305
	public bool animEnabled = true;

	// Token: 0x0400051A RID: 1306
	public bool animPauseOnEnd;

	// Token: 0x0400051B RID: 1307
	public bool initializeOnAwake;

	// Token: 0x0400051C RID: 1308
	public bool animSyncToAudio = true;

	// Token: 0x0400051D RID: 1309
	public float morphSpeed = 0.1f;

	// Token: 0x0400051E RID: 1310
	public bool overrideWeight;

	// Token: 0x0400051F RID: 1311
	public MMD4MecanimAnimMorphHelper.Anim[] animList;

	// Token: 0x04000520 RID: 1312
	private bool _initialized;

	// Token: 0x04000521 RID: 1313
	private MMD4MecanimModel _model;

	// Token: 0x04000522 RID: 1314
	private AudioSource _audioSource;

	// Token: 0x04000523 RID: 1315
	private MMD4MecanimAnimMorphHelper.Anim _playingAnim;

	// Token: 0x04000524 RID: 1316
	private float _animTime;

	// Token: 0x04000525 RID: 1317
	private float _morphWeight;

	// Token: 0x04000526 RID: 1318
	private float _weight2;

	// Token: 0x04000527 RID: 1319
	private HashSet<MMD4MecanimModel.Morph> _inactiveModelMorphSet = new HashSet<MMD4MecanimModel.Morph>();

	// Token: 0x020000A1 RID: 161
	[Serializable]
	public class Anim : MMD4MecanimAnim.IAnim
	{
		// Token: 0x17000093 RID: 147
		// (get) Token: 0x060003BA RID: 954 RVA: 0x00019BD8 File Offset: 0x00017DD8
		// (set) Token: 0x060003BB RID: 955 RVA: 0x00002973 File Offset: 0x00000B73
		string MMD4MecanimAnim.IAnim.animatorStateName
		{
			get
			{
				return null;
			}
			set
			{
			}
		}

		// Token: 0x17000094 RID: 148
		// (get) Token: 0x060003BC RID: 956 RVA: 0x0001D4CA File Offset: 0x0001B6CA
		// (set) Token: 0x060003BD RID: 957 RVA: 0x00002973 File Offset: 0x00000B73
		int MMD4MecanimAnim.IAnim.animatorStateNameHash
		{
			get
			{
				return 0;
			}
			set
			{
			}
		}

		// Token: 0x17000095 RID: 149
		// (get) Token: 0x060003BE RID: 958 RVA: 0x0001D4CD File Offset: 0x0001B6CD
		// (set) Token: 0x060003BF RID: 959 RVA: 0x0001D4D5 File Offset: 0x0001B6D5
		TextAsset MMD4MecanimAnim.IAnim.animFile
		{
			get
			{
				return this.animFile;
			}
			set
			{
				this.animFile = value;
			}
		}

		// Token: 0x17000096 RID: 150
		// (get) Token: 0x060003C0 RID: 960 RVA: 0x0001D4DE File Offset: 0x0001B6DE
		// (set) Token: 0x060003C1 RID: 961 RVA: 0x0001D4E6 File Offset: 0x0001B6E6
		AudioClip MMD4MecanimAnim.IAnim.audioClip
		{
			get
			{
				return this.audioClip;
			}
			set
			{
				this.audioClip = value;
			}
		}

		// Token: 0x17000097 RID: 151
		// (get) Token: 0x060003C2 RID: 962 RVA: 0x0001D4EF File Offset: 0x0001B6EF
		// (set) Token: 0x060003C3 RID: 963 RVA: 0x0001D4F7 File Offset: 0x0001B6F7
		MMD4MecanimData.AnimData MMD4MecanimAnim.IAnim.animData
		{
			get
			{
				return this.animData;
			}
			set
			{
				this.animData = value;
			}
		}

		// Token: 0x17000098 RID: 152
		// (get) Token: 0x060003C4 RID: 964 RVA: 0x0001D500 File Offset: 0x0001B700
		// (set) Token: 0x060003C5 RID: 965 RVA: 0x0001D508 File Offset: 0x0001B708
		MMD4MecanimAnim.MorphMotion[] MMD4MecanimAnim.IAnim.morphMotionList
		{
			get
			{
				return this.morphMotionList;
			}
			set
			{
				this.morphMotionList = value;
			}
		}

		// Token: 0x04000529 RID: 1321
		public string animName;

		// Token: 0x0400052A RID: 1322
		public TextAsset animFile;

		// Token: 0x0400052B RID: 1323
		public AudioClip audioClip;

		// Token: 0x0400052C RID: 1324
		[NonSerialized]
		public MMD4MecanimData.AnimData animData;

		// Token: 0x0400052D RID: 1325
		[NonSerialized]
		public MMD4MecanimAnim.MorphMotion[] morphMotionList;
	}
}
